package com.qst.crop.controller;


import com.github.pagehelper.PageInfo;
import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.Export;
import com.qst.crop.entity.Order;
import com.qst.crop.entity.Question;
import java.lang.Integer;
import java.util.List;

import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.ExportService;
import com.qst.crop.service.OrderService;
import com.qst.crop.service.QuestionService;
import com.qst.crop.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;
    @Autowired
    UserService userService;

    @Resource
    ExportService exportService;
    //分页获取所有的all1111111
    @GetMapping("/findAllQues/{pageNum}")
    public Result<PageInfo> findAllQues(@PathVariable("pageNum")Integer pageNum){
        PageInfo<Question> pageInfo = questionService.findAllQues(pageNum);
//        JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication();
//        System.out.println(jwtUser.getUsername());
        return new Result<PageInfo>(true, StatusCode.OK,"查询成功",pageInfo);
    }

    @GetMapping("/findPageQues/{keys}/{pageNum}")
    public Result<PageInfo> findPageQues(@PathVariable("keys")String keys,@PathVariable("pageNum")Integer pageNum){
        PageInfo<Question> pageInfo = questionService.findPageQues(keys,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK,"查询成功",pageInfo);
    }


    //TODO
    //网络有数据但是网页显示不出来
    @GetMapping("/selectId/{id}")
    public Result<Question> selectId(@PathVariable("id")Integer id){
        return new Result<>(true,StatusCode.OK,"查询成功",questionService.selectId(id));
    }

    @PostMapping("/add")
    public Result add(@RequestBody Question question){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        question.setQuestioner(username);
        return new Result<>(true,StatusCode.OK,"插入成功",questionService.add(question));
    }

    @GetMapping("/findExpert/{pageNum}")
    public Result<PageInfo> findExport(@PathVariable("pageNum")Integer pageNum){
        PageInfo<Export> pageInfo = exportService.findExport(pageNum);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    @GetMapping("/findExportByKeys/{keys}/{pageNum}")
    public Result<PageInfo> findExportByKeys(@PathVariable("keys")String keys,@PathVariable("pageNum")Integer pageNum){
        PageInfo<Export> pageInfo = exportService.findExportByKeys(keys,pageNum);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }


    @GetMapping("/selectByKind/{role}")
    public Result<List<Question>> selectByKind(@PathVariable("role") String role){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        Result<List<Question>> rst = null;
        if(role.equals("questioner")){
            rst = new Result<>(true,StatusCode.OK,"查询成功", questionService.selectByKindAsUser(username));
        }
        else if(role.equals("expert")){
            rst = new Result<>(true,StatusCode.OK,"查询成功", questionService.selectByKindAsExport(username));
        }
        return rst;
    }

    @PutMapping("/update")
    public Result<Integer> update(@RequestBody Question question){
//        String answer = question.getAnswer().trim();
//        if (answer.equals("")){
//            answer = null;
//        }
//        question.setAnswer(answer);
        return new Result<>(true,StatusCode.OK,"更新成功", questionService.update(question));

    }

    @DeleteMapping("/delete/{id}")
    public Result<Integer> delById(@PathVariable("id") Integer id){

        return new Result<>(true,StatusCode.OK,"删除成功", questionService.delById(id));
    }


}
